package com.bd.scm.module.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.scm.dao.mapper.system.BdSysProcDescMapper;
import com.bd.scm.domain.dto.system.ColumnDto;
import com.bd.scm.domain.models.department.DtDep;
import com.bd.scm.domain.models.system.BdSysProcDesc;
import com.bd.scm.module.common.BaseServiceImpl;
import com.bd.scm.module.enums.ExceptionEmun;
import com.bd.scm.module.exception.ScmException;
import com.bd.scm.module.system.IBdSysProcDescService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.DateTimeKit;
import com.bd.utils.exception.BdException;
import com.bd.utils.vo.Result;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yufc
 * @since 2019-12-06
 */
@Service
public class BdSysProcDescServiceImpl extends BaseServiceImpl<BdSysProcDescMapper, BdSysProcDesc> implements IBdSysProcDescService {

    @Autowired
    private BdSysProcDescMapper bdSysProcDescMapper;



    /**
     * 根据类型ID获取字段信息（DTO）
     * @param classId   类型ID
     * @return
     */
    @Override
    public List<ColumnDto> getColumnsByClassId(int classId) {

        List<BdSysProcDesc> colList = getColumnsListByClassId(classId);

        if (CollectionUtils.isEmpty(colList)) {
            return null;
        }

        List<ColumnDto> clientColumns = getClientColumns(colList);

        return clientColumns;
    }

    /**
     * 获取排班列字段
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<ColumnDto> getColumnsSchedule(int year, int month) {

        if(!DateTimeKit.validYear(year)) {
            throw new ScmException(ExceptionEmun.SERVICE_ERROR_PARAMS);
        }

        if(!DateTimeKit.validMonth(month)) {
            throw new ScmException(ExceptionEmun.SERVICE_ERROR_PARAMS);
        }

        List<BdSysProcDesc> colList = getColumnsListByClassId(6);

        if (CollectionUtils.isEmpty(colList)) {
            return null;
        }

        List<ColumnDto> clientColumns = getClientColumns(colList);

        int lastDay = DateTimeKit.getLastDayOfMonth(year, month);

        for (int i = 0; i < clientColumns.size(); i++) {
            String strDay = clientColumns.get(i).getHeaderName();
            if(StringUtils.isNumeric(strDay)) {
                int day = Integer.parseInt(strDay);
                String weekName = DateTimeKit.getWeekOfDate(year, month, day);
                clientColumns.get(i).setHeaderName(String.format("%s/%s", strDay, weekName));
            }
        }

        return clientColumns;
    }


    /**
     * 根据类型ID获取字段信息
     *
     * @param classId 类型ID
     * @return
     */
    public List<BdSysProcDesc> getColumnsListByClassId(int classId) {
        QueryWrapper<BdSysProcDesc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_class_id", classId).orderByAsc("prop_id");

        List<BdSysProcDesc> list = bdSysProcDescMapper.selectList(queryWrapper);

        return list;
    }

    private List<ColumnDto> getClientColumns(List<BdSysProcDesc> colList) {
        List<BdSysProcDesc> listColumn = colList.stream().filter(a -> a.getMultiColumnId() == 0).collect(Collectors.toList());
        List<BdSysProcDesc> listChild = colList.stream().filter(a -> a.getMultiColumnId() > 0).collect(Collectors.toList());

        return getClientColumns(listColumn, listChild);
    }

    /**
     * 生成前端ag-grid列绑定数据
     *
     * @param colList
     * @return
     */
    private List<ColumnDto> getClientColumns(List<BdSysProcDesc> colList, List<BdSysProcDesc> groupColList) {

        List<ColumnDto> listRtn = new ArrayList<>();

        colList.forEach(row -> {
            ColumnDto col = new ColumnDto();

            if (row.getIsMultiColumn() == 1 && row.getMultiColumnId() == 0) {
                List<BdSysProcDesc> childs = groupColList.stream().filter(a -> a.getMultiColumnId().equals(row.getId())).sorted(Comparator.comparing(BdSysProcDesc::getPropId)).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(childs)) {
                    col.setChildren(getClientColumns(childs, groupColList));
                }

//                col.setColId(row.getColumnName());
                col.setHeaderName(row.getColumnTitle());
//                col.setField(row.getColumnName());
//                col.setType(row.getColumnType());
//                col.setWidth(row.getWidth());
//                col.setHide(row.getIsDisplay() == 1 ? false : true);
//                col.setPinned(row.getPinned());
//                col.setSortable(row.getIsSortable() == 1 ? true : false);
//                col.setEditable(row.getIsEditable() == 1 ? true : false);
//                col.setSuppressMenu(row.getMenuEnable() == 1 ? false : true);
//                col.setCellRenderer(row.getCellRenderer());
//                col.setTooltipField(row.getTooltipField());
            } else {
                col.setColId(row.getColumnName());
                col.setHeaderName(row.getColumnTitle());
                col.setField(row.getColumnName());
                col.setType(row.getColumnType());
                col.setWidth(row.getWidth());
                col.setHide(row.getIsDisplay() == 1 ? false : true);
                col.setPinned(row.getPinned());
                col.setSortable(row.getIsSortable() == 1 ? true : false);
                col.setEditable(row.getIsEditable() == 1 ? true : false);
                col.setSuppressMenu(row.getMenuEnable() == 1 ? false : true);
                col.setCellRenderer(row.getCellRenderer());
                col.setTooltipField(row.getTooltipField());
            }

            listRtn.add(col);
        });

        return listRtn;
    }
}
